(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.b.piLoad.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','piLoadService', 'commonService','$filter' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService,piLoadService, commonService, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("piLoadList");
		ctrl.layoutConfig = layoutConfig;

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.loadStatusOptions = getLoadStatus().data;
			ctrl.search();
		};
		
		function getLoadStatus() {
			var options = {
					"data" : [{
						"label" : "label.loadstatus.c",
						"value" : "C"
					},{
						"label" : "label.loadstatus.w",
						"value" : "W"    
					}, {
						"label" : "label.loadstatus.e",
						"value" : "E"    
					}, {
						"label" : "label.loadstatus.i",
						"value" : "I"    
					},{
						"label" : "label.loadstatus.j",
						"value" : "J"    
					}, {
						"label" : "label.loadstatus.t",
						"value" : "T"    
					}, {
						"label" : "label.loadstatus.r",
						"value" : "R"    
					}, {
						"label" : "label.loadstatus.x",
						"value" : "X"    
					}, {
						"label" : "label.txnstatus.l",
						"value" : "L"    
					}, {
						"label" : "label.txnstatus.m",
						"value" : "M"    
					}]
				};
			return options;
		}

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};
		ctrl.summary = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('apf.b.piLoad.pi.list', {piloadId : $filter('encode')(ctrl.selectedRows[0].piloadId)});
		};

		ctrl.piErrorList = function (rowEntity){
			modalService.openCustomDialogs({
			   'title': 'Error Payments of Load ' + $filter('encode')(rowEntity.piloadId),
			   'dialogsModule' : 'apf.piError.piErrorList',
			   'params': { piloadId : $filter('encode')(rowEntity.piloadId) },
			   'size' : [1344],
			   'btnCallback' : {
				}	
			});
		};
		
		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
		
		ctrl.downloadSourcefile = function(rowObj) {
			var param  = {};
			param.piloadId = rowObj.piloadId;
			param.siteCode = rowObj.siteCode;
			param.srcFileName = rowObj.srcFileName;
			param.isTemporary = "N";
			commonService.download(param, 'do/apf/b/piLoad/download');
		};
		ctrl.downloadAck1 = function(rowObj) {
			var param  = {};
			param.piloadId = rowObj.piloadId;
			param.siteCode = rowObj.siteCode;
			param.resFileName = rowObj.resFileName;
			param.isTemporary = "N";
			commonService.download(param, 'do/apf/b/piLoad/download');
		};
		ctrl.downloadAck2 = function(rowObj) {
			var param  = {};
			param.piloadId = rowObj.piloadId;
			param.siteCode = rowObj.siteCode;
			param.resFileName2 = rowObj.resFileName2;
			param.isTemporary = "N";
			commonService.download(param, 'do/apf/b/piLoad/download');
		};
	}
})(angular);










